#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import sys

from common.process_handle import process_handle
from common.common import exter_attack

from ndmp.comm.log import log
from ndmp.service.resource.ndmp_resource_service import NDMPResouceService
from ndmp.service.backup.ndmp_backup_service import NDMPBackupService
from ndmp.service.restore.ndmp_restore_service import NDMPRestoreService
from ndmp.service.index.ndmp_index_service import NDMPIndexService


class NDMPApi:
    @staticmethod
    @exter_attack
    def check_application_server(req_id, job_id, sub_id, data):
        NDMPResouceService(req_id, job_id, sub_id).check_application_server()

    @staticmethod
    @exter_attack
    def check_application(req_id, job_id, sub_id, data):
        NDMPResouceService(req_id, job_id, sub_id).check_application()

    @staticmethod
    @exter_attack
    def list_application_resource_v2(req_id, job_id, sub_id, data):
        NDMPResouceService(req_id, job_id, sub_id).list_application_resource_v2()

    @staticmethod
    @exter_attack
    def query_job_permission(req_id, job_id, sub_id, data):
        NDMPResouceService(req_id, job_id, sub_id).query_job_permission()

    @staticmethod
    @exter_attack
    def allow_backup_in_local_node(req_id, job_id, sub_id, data):
        NDMPBackupService(req_id, job_id, sub_id).allow_backup_in_local_node()

    @staticmethod
    @exter_attack
    def check_backup_job_type(req_id, job_id, sub_id, data):
        NDMPBackupService(req_id, job_id, sub_id).check_backup_job_type()

    @staticmethod
    @exter_attack
    def backup_prerequisite(req_id, job_id, sub_id, data):
        NDMPBackupService(req_id, job_id, sub_id).backup_prerequisite()

    @staticmethod
    @exter_attack
    def backup_prerequisite_progress(req_id, job_id, sub_id, data):
        NDMPBackupService(req_id, job_id, sub_id).backup_prerequisite_progress()

    @staticmethod
    @exter_attack
    def backup_gen_sub_job(req_id, job_id, sub_id, data):
        NDMPBackupService(req_id, job_id, sub_id).backup_gen_sub_job()

    @staticmethod
    @exter_attack
    def backup(req_id, job_id, sub_id, data):
        NDMPBackupService(req_id, job_id, sub_id).backup()

    @staticmethod
    @exter_attack
    def backup_post_job(req_id, job_id, sub_id, data):
        NDMPBackupService(req_id, job_id, sub_id).backup_post_job()

    @staticmethod
    @exter_attack
    def backup_post_job_progress(req_id, job_id, sub_id, data):
        NDMPBackupService(req_id, job_id, sub_id).backup_post_job_progress()

    @staticmethod
    @exter_attack
    def query_backup_copy(req_id, job_id, sub_id, data):
        NDMPBackupService(req_id, job_id, sub_id).query_backup_copy()

    @staticmethod
    @exter_attack
    def abort(req_id, job_id, sub_id, data):
        NDMPBackupService(req_id, job_id, sub_id).abort_backup_job()

    @staticmethod
    @exter_attack
    def generate_index(req_id, job_id, sub_id, data):
        NDMPIndexService(req_id, job_id, sub_id).generate_index()

    @staticmethod
    @exter_attack
    def allow_restore_in_local_node(req_id, job_id, sub_id, data):
        NDMPRestoreService(req_id, job_id, sub_id).allow_restore_in_local_node()

    @staticmethod
    @exter_attack
    def restore_prerequisite(req_id, job_id, sub_id, data):
        NDMPRestoreService(req_id, job_id, sub_id).restore_prerequisite()

    @staticmethod
    @exter_attack
    def restore_prerequisite_progress(req_id, job_id, sub_id, data):
        NDMPRestoreService(req_id, job_id, sub_id).restore_prerequisite_progress()

    @staticmethod
    @exter_attack
    def restore_gen_sub_job(req_id, job_id, sub_id, data):
        NDMPRestoreService(req_id, job_id, sub_id).restore_gen_sub_job()

    @staticmethod
    @exter_attack
    def restore(req_id, job_id, sub_id, data):
        NDMPRestoreService(req_id, job_id, sub_id).restore()

    @staticmethod
    @exter_attack
    def restore_post_job(req_id, job_id, sub_id, data):
        NDMPRestoreService(req_id, job_id, sub_id).restore_post_job()

    @staticmethod
    @exter_attack
    def restore_post_job_progress(req_id, job_id, sub_id, data):
        NDMPRestoreService(req_id, job_id, sub_id).restore_post_job_progress()

fun = {
    'CheckApplicationServer': NDMPApi.check_application_server,
    'CheckApplication': NDMPApi.check_application,
    'ListApplicationResourceV2': NDMPApi.list_application_resource_v2,

    'AllowBackupInLocalNode': NDMPApi.allow_backup_in_local_node,
    'CheckBackupJobType': NDMPApi.check_backup_job_type,
    'BackupPrerequisite': NDMPApi.backup_prerequisite,
    'BackupPrerequisiteProgress': NDMPApi.backup_prerequisite_progress,
    'BackupGenSubJob': NDMPApi.backup_gen_sub_job,
    'Backup': NDMPApi.backup,
    'BackupPostJob': NDMPApi.backup_post_job,
    'BackupPostJobProgress': NDMPApi.backup_post_job_progress,
    'QueryBackupCopy': NDMPApi.query_backup_copy,
    'AbortJob': NDMPApi.abort,
    'CreateIndex': NDMPApi.generate_index,

    'QueryJobPermission': NDMPApi.query_job_permission,

    'AllowRestoreInLocalNode': NDMPApi.allow_restore_in_local_node,
    'RestorePrerequisite': NDMPApi.restore_prerequisite,
    'RestorePrerequisiteProgress': NDMPApi.restore_prerequisite_progress,
    'RestoreGenSubJob': NDMPApi.restore_gen_sub_job,
    'Restore': NDMPApi.restore,
    'RestorePostJob': NDMPApi.restore_post_job,
    'RestorePostJobProgress': NDMPApi.restore_post_job_progress,
}

# NDMP Plugin API 入口
if __name__ == '__main__':
    """
    功能描述:执行NDMP插件主程序
    参数:
    @1: 请求类型
    @2: 请求的ID
    @3: 主任务ID
    @4: 子任务ID
    输入:
    标准输入数据为外部程序通过管道向插件程序注入的各项参数数据（可能携带敏感数据）
    样例:
    python3 ndmp_api.py Backup 1657539915784 2e0c6c0b faa3e2c0
    """
    # 打印日志
    log.info('Enter NDMP Plugin process')
    process_handle(log, sys.argv, sys.stdin, fun)
    log.info('Exit NDMP Plugin process')
